//
//  ICVoiceHud.m
//  ICome
//
//  Created by ENN on 16/5/18.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICVoiceHud.h"
#import "ICHeaderFile.h"

@interface ICVoiceHud ()
{
    NSArray *_images;
}
@end

@implementation ICVoiceHud


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.animationDuration    = 0.5;
        self.animationRepeatCount = -1;
        _images                   = @[
                                      [UIImage imageNamedFromiComeSDKBundle:@"icon_voice_1"],
                                      [UIImage imageNamedFromiComeSDKBundle:@"icon_voice_2"],
                                      [UIImage imageNamedFromiComeSDKBundle:@"icon_voice_3"],
                                      [UIImage imageNamedFromiComeSDKBundle:@"icon_voice_4"],
                                      [UIImage imageNamedFromiComeSDKBundle:@"icon_voice_5"],
                                      [UIImage imageNamedFromiComeSDKBundle:@"icon_voice_6"]
                                      ];
    }
    return self;
}


- (void)setProgress:(CGFloat)progress
{
    _progress = MIN(MAX(progress, 0.f),1.f);
    [self updateImages];
}

- (void)setCountDown:(CGFloat)countDown
{
    _countDown = countDown;
    [self updateCountDown];
}

- (void)updateImages
{
    [self.countDownLabel removeFromSuperview];
    if (_progress == 0) {
        self.animationImages = nil;
        [self stopAnimating];
        return;
    }
    if (_progress >= 0.8 ) {
        self.animationImages = @[_images[3],_images[4],_images[5],_images[4],_images[3]];
    } else {
        self.animationImages = @[_images[0],_images[1],_images[2],_images[1]];
    }
    [self startAnimating];
}

- (void)updateCountDown
{
    self.animationImages = nil;
    [self stopAnimating];
    [self addSubview:self.countDownLabel];
    self.countDownLabel.text = [NSString stringWithFormat:@"%d", (int)_countDown];
}

#pragma mark - Getter

- (UILabel *)countDownLabel {
    if (!_countDownLabel) {
        _countDownLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.width-20, self.height-50)];
        _countDownLabel.textAlignment = NSTextAlignmentCenter;
        _countDownLabel.textColor = [UIColor whiteColor];
        _countDownLabel.font = ICFont(70);
    }
    return _countDownLabel;
}

@end
